package model.fileModel;

import empty.files.MyFile;
import unit.DbTool;

import java.sql.SQLException;
import java.util.List;

public class FileModel {
    public List<Object[]> selectCoffer() throws SQLException {
        String sql = "select a.*,b.name father_name from " +
                "(select f.*,u.name user_name from files f left join users u on f.create_user = u.id where father='-1') " +
                "a left join files b " +
                "on a.father=b.id;";
        return DbTool.DQL(sql);
    }

    public List<Object[]> selectFileAndDir(Object[] objs) throws SQLException {
        String sql = "select a.*,b.name father_name from " +
                "(select f.*,u.name user_name from files f left join users u on f.create_user = u.id where father = ? ) " +
                "a left join files b " +
                "on a.father=b.id order by a.type;";
        return DbTool.DQL(sql,objs[0]);
    }

    public int insertCoffer(MyFile myFile) throws SQLException{
        String sql = "insert into files(name, father, type, create_time, create_user) VALUES (?,-1,'保险箱',?,?);";
        return DbTool.DML(sql, myFile.getName(), myFile.getCreate_time(), myFile.getCreate_user());
    }

    public int insertFileAndDir(MyFile myFile) throws SQLException{
        String sql = "insert into files(name, father, type, create_time, create_user) VALUES (?,?,?,?,?);";
        return DbTool.DML(sql, myFile.getName(), myFile.getFather(), myFile.getType(), myFile.getCreate_time(), myFile.getCreate_user());
    }

    public int deleteFile(MyFile myFile) throws SQLException{
        String sql = "delete from files where id=? or father =?";
        return DbTool.DML(sql,myFile.getId(),myFile.getId());
    }

    public int updateFile(MyFile myFile) throws SQLException{
        String sql = "update files set name=? where id=?;";
        return DbTool.DML(sql,myFile.getName(),myFile.getId());
    }

    public List<Object[]> SelectQxById(int user_id, int file_id) throws SQLException {
        String sql = "select file_qx from permissions where user_id=? and file_id=?;";
        return DbTool.DQL(sql,user_id,file_id);
    }
}
